package com.abc.core.service.impl;

import com.abc.core.def.Dna;
import com.abc.core.service.DnaCacheService;

import java.util.concurrent.ConcurrentHashMap;

public class DnaCacheServiceImpl implements DnaCacheService {
    private ConcurrentHashMap<String, Dna> dnaMap = new ConcurrentHashMap<>(512);
    private ConcurrentHashMap<String, Dna> childDnaMap = new ConcurrentHashMap<>(512);
    private ConcurrentHashMap<String, InstanceDnaCache> otherDnaCacheServiceMap = new ConcurrentHashMap<>(512);

    @Override
    public Dna getDna(String dnaCode) {
        if (dnaCode == null) {
            throw new RuntimeException("dnaCode不能为空");
        }
        Dna dna = dnaMap.get(dnaCode);
        if (dna != null) {
            return dna;
        }

        dna = childDnaMap.get(dnaCode);
        if (dna != null) {
            return dna;
        }

        for (InstanceDnaCache dnaCacheService : otherDnaCacheServiceMap.values()) {
            dna = dnaCacheService.getDna(dnaCode);
            if (dna != null) {
                return dna;
            }
        }
        throw new RuntimeException("Dna not exists!");
    }

}
